<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.267
 */

/**
 * Description of FunctionsClass
 *
 * @author seth
 */
class Utils {

    var $USE_URL_PARAM, $ADMIN_HOME;
  function processURL($cattype, $pageid, $title, $directlink, $parent="") {
        global $cquery,$HOME_URL;

        if (strlen($directlink) > 10) {
            $url = $directlink;
        } else {
              $url=$HOME_URL.$cquery->pageRewriteUrl($pageid, $cattype, $title, $pageid, $directlink);
//            $title = $this->urlFriendly($title);
//            if (strlen($parent) > 1)
//                $parent = $this->urlFriendly($parent) . "/";
//            if ($USE_URL_PARAM == "no") {
//                if ($cattype != "tags")
//                    $title = "/" . $title;
//                else {
//                    $pageid = $title;
//                    $title = "";
//                }
//                $url = $HOME_URL . $SITE_NAME . "/" . $cattype . "/" . $parent . $pageid . $title;
//            } else {
//                $url = $HOME_URL . "?" . $title . "&amp;" . $cattype . "=" . $pageid . "&amp;type=" . $parent . "&amp;site=" . $SITE_NAME;
//            }
        }
        return $url;
    }

    function processOtherURL($cattype, $title, $direclink) {
        if ($this->doDirecLink($directLink))
            $url = $direclink;
        else {
            $title = $this->urlFriendly($title);
            if ($USE_URL_PARAM == "no") {
                $url = $HOME_URL . $SITE_NAME . "/" . $cattype . "/" . $title;
            } else {
                $url = $HOME_URL . "?" . $cattype . "=" . $title . "&site=" . $SITE_NAME;
            }
        }
    }

    function processSpecialURL($cattype, $pageid, $title, $directlink, $HOME_URL) {
        global $USE_URL_PARAM;
        global $SITE_NAME;
        if (strlen($directlink) > 10) {
            $url = $directlink;
        } else {
            $title = $this->urlFriendly($title);
            if ($USE_URL_PARAM == "no") {
                $url = $HOME_URL . $SITE_NAME . "/" . $cattype . "/" . $pageid . "/" . $title;
            } else {
                $url = $HOME_URL . "?" . $title . "&" . $cattype . "=" . $pageid . "&site=" . $SITE_NAME;
            }
        }
        return $url;
    }

    function getHomeLink($home_url, $site_name) {
        global $USE_URL_PARAM;
        if ($USE_URL_PARAM == "no") {
            $url = $home_url ;//. $site_name;
        } else {
            $url = $home_url;// . "?site=" . $site_name;
        }
        $url = $home_url;// . "?site=" . $site_name;
        return $url;
    }

    function redirect($url) {
        $p = '<html><head><meta http-equiv="Refresh" content="00; url=' . $url . '"></head></html>';
        echo $p;
        exit ();
    }

    function getSmallContents($content, $offset) {
        $rr = strip_tags($content, "br");

        if (strlen($rr) <= $offset) {
            $str = $rr;
        } else {
            $sttr = substr($rr, 0, $offset);

            $startFrom = 0;
            $lastDot = strrpos($sttr, ".");
            $spaceDot = strrpos($sttr, " ");
            if ($lastDot > $spaceDot)
                $to = $lastDot; else
                $to=$spaceDot;

            $str = substr($sttr, 0, $to);
        }
        if (strlen($str) == 0) {
            if (strlen($rr == 0))
                $str = "";
            else
                $str = substr($rr, 0, @strpos($rr, " ", $offset));
        }
        return $str;
    }

    function urlFriendly($location, $replaceWith="-") {
        return strtolower($this->replaceSpecialCharacters($location, $replaceWith));
    }

    function replaceSpecialCharacters($location, $replaceWith="-") {
        $loc=  (preg_replace("[\W]", $replaceWith, $location));
        $loc = (preg_replace("[--]", $replaceWith, $loc)); return (preg_replace("[--]", $replaceWith, $loc));
    }

    function validateNum($int, $default) {
        if (!is_numeric($int)) {
            $int = $default;
        }
        return $int;
    }

    function doDirecLink($directLink) {
        $length = strlen($directLink);
        if ($length > 3) {
            return true;
        } else if ($length == 1 && $directLink == "#") {
            return true;
        }
        return false;
    }

    function checkOutOfRange($recordoffset, $total) {
        if (($recordoffset) > $total) {
            $offset = 0;
            return false;
        }else
            return true;
    }

    function tableName($tabName) {
        global $tablePrefix;
        return $tablePrefix . $tabName;
    }

    function pageExtraInfo($listtype, $datecreated, $lastmodified, $type, $src="") {
        $div = "";
        $date = explode(":::", $datecreated);

        if ($listtype == 2) {
            if ($type == "short") {
                $div = "<div class='date'>" . substr($date[0], 0, strrpos($date[0], ",")) . "</div>";
            } else {

                $div = "<div class='date'>Date :" . $date[0] . "</div>";
                if (strlen($src) > 0)
                    $div.= "<div class='source date'>Source: " . $src . "</div>";
            }
        }
        //event
        else if ($listtype == 3) {
            $date2 = split(":::", $lastmodified);
            $d = "";
            if ($date[0] != $date2["0"]) {
                if ($type == "short")
                    $d.=substr($date[0], 0, strrpos($date[0], ",")) . " - " . substr($date2[0], 0, strrpos($date2[0], ","));
                else
                    $d.=substr($date[0], 0, strrpos($date[0], ",")) . " - " . $date2[0];
            }else {
                if ($type == "short")
                    $d.= substr($date[0], 0, strrpos($date[0], ","));
                else
                    $d.=$date2[0];
            }
            if ($type == "short") {
                $d.="<div class='smaller'>" . strtolower($date[1]) . "</div>";
            }else
                $d.="<br />From:" . strtolower($date[1] . " - " . $date2[1]);

            $div = "<div class='date'>" . $d . "</div>";
        }
        return $div;
    }

    function processBanners($resourceurl, $alt) {


        $fileExtention = strtolower(substr($resourceurl, strrpos($resourceurl, ".") + 1));

        //Check whether is it an image
        if ($fileExtention == "gif" || $fileExtention == "png" || $fileExtention == "jpg") {
            $t = $this->processImageBanner($resourceurl, $alt);
        }
        //process banners
        else if ($fileExtention == "swf") {
            $t = $this->processFlash($resourceurl);
        }
        //there is no banner
        else {
            $t = " NO Banner ";
        }
        return $t;
    }

    //processing  a flash banner
    function processFlash($url, $bannerWidth=985, $bannerHeight=150) {
        return $this->processSWF($url, $bannerWidth, $bannerHeight, "adv_banner_swf");
    }

    //processing resources
    function processResources($resourceurl, $alt, $width=200, $height=150) {
        $t="";
        $fileExtention = strtolower(substr($resourceurl, strrpos($resourceurl, ".") + 1));

        if ($fileExtention == "gif" || $fileExtention == "png" || $fileExtention == "jpg") {
            $t = $this->processImageBanner($resourceurl, $alt);
        } else if ($fileExtention == "swf") {
            $t = $this->processSWF($resourceurl, $width, $height);
        } else {
            $t = "No Banner ";
        }
        return $t;
    }

    //flash player
    function processSWF($url, $width, $height, $swfId="") {
        global $HOME_URL;
        $url = $HOME_URL . "resources/" . $url;
        if (strlen($swfId) > 0)
            $divId = $swfId;
        $divId = "swf_" . rand(0, 1000);
        $t = "<div id='$divId'>
                  <h1>Alternative content</h1>
			<p><a href=\"http://www.adobe.com/go/getflashplayer\"><img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash player\" /></a></p>\"

        </div>";
        $t.="<script>
                swfobject.embedSWF(\"$url\", \"$divId\", \"$width\", \"$height\", \"9.0.0\", \"" . $HOME_URL . "js/expressInstall.swf\");
            </script>";
        return $t;
    }

    //Advert Manager
    function showAds($adType,$withTitle=false,$withContent=false) {
        $ul="";$res="";
        global $cquery;
        global $HOME_URL;
        global $SITE_ID;
        $adsId = $cquery->siteAds($SITE_ID, $adType);
        $ads = $cquery->getContentByCatLimit($adsId, 0, 10);
        $ul.="<ul id='adv_ads_$adType'>";
        foreach ($ads as $advert) {
            $ul.="
                <li>";
            $resource = $cquery->getFirstResourceContent($advert["content_id"]);
            $url = $this->processURL("page", $advert["content_id"], $advert["page_title"], $advert["direct_link"]);
            $ul.="<a href='$url' title='" . $advert["page_title"] . "' alt='" . $advert["page_title"] . "'>";
            if (strlen($resource) > 0) {
                //processURL($cattype,$pageid,$title,$directlink,$parent="")

                $res=$this->processResources($resource, $advert["page_title"]);
                $ul.=$res;
                if($withTitle)
                    $ul.="<div class='title'>".$withTitle."</div>";
            } else {
                $ul.= $advert["page_title"];
            }
            if($withContent)
                $ul.=$advert["page_content"];
            $ul.="</a>";
            $ul.="</li>";
        }
        $ul.="</ul>";

        return $ul;
    }

    function processImageBanner($resource, $alt) {
        global $HOME_URL;
        return $this->imageDisplay($HOME_URL."resources/".$resource,$alt);//"<img src='" . $HOME_URL . "resources/" . $resource . "' alt='" . $alttxt . "' title='" . $alttxt . "' />";
    }
    function imageDisplay($resource,$alt){
        if(strlen($resource)>0)
        return "<img src='" . $resource . "' alt='" . $alt . "' title='" . $alt . "' />";

    }

    function zipFiles($files, $destination) {

    }

    function dirCreate($folder, $dirname) {
        if (is_dir($folder . $dirname)) {

        } else {
            mkdir($folder . $dirname, 777);
        }
    }

    function unzipFile($zipfile, $destination) {
        $destination.="/";
        $entry_name = "";
        $zip = zip_open($zipfile);
        if ($zip) {
            while ($zip_entry = zip_read($zip)) {
                try {
                    $f = $destination . zip_entry_name($zip_entry);

                    if (strlen($entry_name) < 1)
                        $entry_name = zip_entry_name($zip_entry);

                    if (strripos($f, ".") < 2) { // There because of ../
                        @mkdir($f, 777);
                        //echo "make Directory ".$f;
                    }
                } catch (Exception $e) {
                    echo $e;
                }

                $fp = @fopen($destination . zip_entry_name($zip_entry), "w");
                if (@zip_entry_open($zip, $zip_entry, "r")) {
                    $buf = @zip_entry_read($zip_entry, @zip_entry_filesize($zip_entry));
                    @fwrite($fp, $buf);
                    @zip_entry_close($zip_entry);
                    @fclose($fp);
                }
            }
            @zip_close($zip);
        }
        return $entry_name;
    }

    //
    function listFileInDirectory($dirname, $fileType="") {
        // Define the full path to your folder from root
        $fileResults = "";
        $path = $dirname;
        // Open the folder
        echo "<li>";
        $dir_handle = @opendir($path) or die("Unable to open $path");

        // Loop through the files
        while ($file = readdir($dir_handle)) {
            if ($file == "." || $file == ".." || $file == "index.php")
                continue;

            // echo "is _dir ".is_dir($path.$file)." -  File - ".($path.$file);
            if (is_dir($path . $file)) {
                //   echo $path.$file." - ".$fileType."<br />";
                echo $file . "<ul>";
                $this->listFileInDirectory($path . $file . "/", $fileType);
                echo "</ul>";
            } else {
                $resourceUrl = $file;
                $fCount = 0;
                if (strlen($fileType) > 2) {
                    if ($this->strEndsWith($file, $fileType))
                        echo "<li><a href='" . $path . $file . "'> $file</a></li>";
                }else {
                    echo "<li><a href='" . $path . $file . "'>~ $file</a></li>";
                }
                // echo "<a href='$file'>Not Valid $file</a><br />";
                //            if(!checkResourceExist($fileType,$folder,$resourceUrl)) {
                //                addResource($fileType,$folder."/".$resourceUrl);
                //                $fileResults.="File added successfully : ".$resourceUrl."<br />";
                //                $fCount=$fCount+1;
                //            }else {
                //
                //            }
                //            }
            }
        }
        echo "</li>";
        if ($fCount == 0

            )$fileResults = " <span class='redtxt'>No New File Found </span>";
        // Close
        closedir($dir_handle);
        return "No of Files : " . $fCount . "<br />Folder : $folder<br />" . $fileResults;
    }

    function strEndsWith($haysack, $needle) {
        $needles = split(",", $needle);
        for ($i = 0; $i < count($needles); $i++) {
            if (strrpos($haysack, $needles[$i]) + strlen($needles[$i]) == strlen($haysack))
                return true;
        }
    }

    function showAdminUpdate($itemid, $cattype="page", $catId=0) {
        global $HOME_URL, $site;
        
        if (isset($_SESSION['maUserAdmin']) && strtolower($site->SHOW_ADMIN_UPDATE) == "yes") {
            $ul = "
                <a href='" . $this->ADMIN_HOME . "itemid=" . $itemid . "' title='Update' class='update' target='_blank'>
                    <img src='" . $HOME_URL . "theme/default/nav/note.gif' />
                </a>
            <a href='" . $this->ADMIN_HOME . "cid=" . $itemid . "' title='Add Page' class='update'>
                   Add Page
                </a>";
            if ($cattype == "cat") {
                $ctlnk = str_replace("content", "category", $this->ADMIN_HOME);
                $ul.= "
            <a href='" . $ctlnk . "pid=" . $catId . "' title='Update' class='update'>
                   Add Category
                </a>";
            }
            if(strlen($ul)>0)
                $ul="<div class='updater'>$ul</div>";

            return $ul;
        }
        else
            return "";
    }


    function twoColorText($text){
        $text = explode(" ", $text);
        $cnt =count($text);
    
        if(($cnt)==1)
        {
            return $text[0];
        }else
        {
            $halfWay = ceil($cnt/2);
         
            $strBuffer="";
            for($i=1;$i<=$cnt;$i++)
            {
                $pos = $i-1;
                $strBuffer.=$text[$pos]." ";
                if($halfWay==$i){
                    $strBuffer.="<span class='pklike'>";

                }
            }
            $strBuffer."</span>";
            return $strBuffer;
        }

    }
}

?>
